﻿using UnityEngine;
using System.Collections;

namespace View
{
    public class ShootParticleSystem
    {

        private ShokwaveParticle shockwave;
        private ExplosionAnimation explosion;
        private SplitterSystem splitter;
        private SmokeSystem smoke;

        private Texture2D shokwaveTexture;
        private Texture2D explosionTexture;
        private Sprite[] explosionSprites;

        private float timeElapsed;
        private float lifeTime = 0;

        public ShootParticleSystem(Vector2 startPos, float radius)
        {
            explosionTexture = Resources.Load<Texture2D>("Textures/ExplosionSprite");
            shokwaveTexture = Resources.Load<Texture2D>("Textures/ShokwaveTexture");
            Sprite[] explosionSprites = Resources.LoadAll<Sprite>("Textures/ExplosionSprite");

            smoke = new SmokeSystem(startPos, radius);
            explosion = new ExplosionAnimation(startPos, 0.7f, explosionSprites, explosionTexture, radius);
            splitter = new SplitterSystem(startPos, radius);
            shockwave = new ShokwaveParticle(startPos, 0.3f, radius);

            lifeTime = 6;
        }

        /// <summary>
        /// Call on this in MasterController.Update() to run view logic.
        /// </summary>
        public void Update()
        {
            if (isAlive())
            {
                timeElapsed += Time.deltaTime;
                shockwave.Update();
                explosion.Update();
                splitter.Update();
                smoke.Update();
            }
        }

        /// <summary>
        /// Call on this in MasterController.OnGUI() to run view logic.
        /// </summary>
        public void OnGUI(GameCamera cam)
        {
            if (isAlive())
            {
                shockwave.OnGUI(cam, shokwaveTexture);
                explosion.OnGUI(cam);
                splitter.OnGUI(cam);
                smoke.OnGUI(cam);
            }
        }

        public bool isAlive()
        {
            if (lifeTime > timeElapsed)
            {
                return true;
            }
            else
            {
                lifeTime = 0;
                return false;
            }
        }
    }
}